package com.log4jviewer.logfile;

/**
 * Class represents converter for special chars.
 * 
 * @author Apache
 * @author <a href="mailto:rd.ryly@gmail.com">Ruslan Diachenko</a>
 */
public final class SpecialCharsConverter {

    private SpecialCharsConverter() {
        // no code
    }

    public static String convertSpecialChars(String literal) {
        int literalLength = literal.length();
        StringBuilder convertedChars = new StringBuilder();

        int i = 0;
        while (i < literalLength) {
            char letter = literal.charAt(i++);
            switch (letter) {
            case '\\':
            case '^':
            case '$':
            case '.':
            case '-':
            case '|':
            case '(':
            case ')':
            case '[':
            case ']':
            case '{':
            case '}':
            case '?':
            case '+':
            case '*':
                convertedChars.append('\\').append(letter);
                break;
            case 'b':
                convertedChars.append('\\').append('b');
                break;
            case 'f':
                convertedChars.append('\\').append('f');
                break;
            case '\t':
                convertedChars.append('\\').append('t');
                break;
            case '\r':
                convertedChars.append("[\\").append('r').append("]?");
                break;
            case '\n':
                convertedChars.append('\\').append('n');
                break;
            default:
                convertedChars.append(letter);
                break;
            }
        }
        return convertedChars.toString();
    }
}
